package com.hutao.sso.controller.authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.sso.pojo.newAuthority.UserRoles;
import com.hutao.sso.pojo.user.Users;
import com.hutao.sso.service.authority.UserRolesService;
import com.hutao.sso.util.Result;
import com.hutao.sso.util.StringUtil;

@RequestMapping("/userRole")
@Controller
public class UserRoleController {
	@Autowired
	private UserRolesService userRoleService;
	
	@PostMapping("/updateUserRoleByUserId")
	@ResponseBody
	public Result updateUserRoleByUserId(String roleStr, String userId, HttpSession session) {
		
		try {
			Map<String, Object> params = new HashMap<>();
			Users user = (Users)session.getAttribute("current_user");
			if(null != user) {
				String medicalMainstayId = user.getMedicalMainstayId();
				List<UserRoles> userRoleList = new ArrayList<>();
				if(StringUtil.isNotBlank(roleStr)) {
					String [] roleArr = roleStr.split(",");
					for(int i = 0; i < roleArr.length; i++) {
						UserRoles userRole = new UserRoles();
						userRole.setRoleId(roleArr[i]);
						userRole.setUserId(userId);
						userRole.setMedicalMainstayId(medicalMainstayId);
						userRoleList.add(userRole);
					}
					
				}
				
				params.put("userId", userId);
				params.put("medicalMainstayId", medicalMainstayId);
				return userRoleService.insertUserRolesList(userRoleList, params);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "分配角色失败!");
		}
		return new Result(false, "分配角色失败!");
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
